home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 123 / MacAddict_123_2006_11.iso / Software / Utilities / Local Weather 1.1.0.9.wdgt / AppleClasses / AppleSlider.js < prev   
Text File  |  2005-11-29  |  15KB  |  478 lines

  1. /*
  2. ¬© Copyright 2005 Apple Computer, Inc. All rights reserved.
  3.  
  4. IMPORTANT:  This Apple software and the associated images located in
  5. /System/Library/WidgetResources/AppleClasses/ (collectively "Apple Software")
  6. are supplied to you by Apple Computer, Inc. (‚ÄúApple‚Äù) in consideration of your
  7. agreement to the following terms. Your use, installation and/or redistribution
  8. of this Apple Software constitutes acceptance of these terms. If you do not
  9. agree with these terms, please do not use, install, or redistribute this Apple
  10. Software.
  11.  
  12. In consideration of your agreement to abide by the following terms, and subject
  13. to these terms, Apple grants you a personal, non-exclusive license, under
  14. Apple‚Äôs copyrights in the Apple Software, to use, reproduce, and redistribute
  15. the Apple Software, in text form (for JavaScript files) or binary form (for
  16. associated images), for the sole purpose of creating Dashboard widgets for Mac
  17. OS X.
  18.  
  19. If you redistribute the Apple Software, you must retain this entire notice and
  20. the warranty disclaimers and limitation of liability provisions (last two
  21. paragraphs below) in all such redistributions of the Apple Software.
  22.  
  23. You may not use the name, trademarks, service marks or logos of Apple to endorse
  24. or promote products that include the Apple Software without the prior written
  25. permission of Apple. Except as expressly stated in this notice, no other rights
  26. or licenses, express or implied, are granted by Apple herein, including but not
  27. limited to any patent rights that may be infringed by your products that
  28. incorporate the Apple Software or by other works in which the Apple Software may
  29. be incorporated.
  30.  
  31. The Apple Software is provided on an "AS IS" basis.  APPLE MAKES NO WARRANTIES,
  32. EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
  33. NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
  34. REGARDING THE APPPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION
  35. WITH YOUR PRODUCTS.
  36.  
  37. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  38. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR DISTRIBUTION OF THE
  41. APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  42. (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  43. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.  */
  45.  
  46. function AppleSlider(slider, onchanged)
  47. {
  48. }
  49.  
  50. /*
  51.  * init() member function
  52.  * Initialize the slider.
  53.  * You do not need to call this directly, it will be called when necessary.
  54.  * You probably want to be calling refresh().
  55.  * pre: this.slider
  56.  * post: this._thumb, this._track + event handlers
  57.  */
  58. AppleSlider.prototype._init = function()
  59. {
  60.     // For JavaScript event handlers
  61.     var _self = this;
  62.     
  63.     this._captureEventHandler = function(event) { _self._captureEvent(event); };
  64.     this._mousedownThumbHandler = function(event) { _self._mousedownThumb(event); };
  65.     this._mousemoveThumbHandler = function(event) { _self._mousemoveThumb(event); };
  66.     this._mouseupThumbHandler = function(event) { _self._mouseupThumb(event); };
  67.     this._mousedownTrackHandler = function(event) { _self._mousedownTrack(event); };
  68.     
  69.     var style = null;
  70.     var element = null;
  71.     
  72.     // Slider Track
  73.     this._track = document.createElement("div");
  74.     style = this._track.style;
  75.     // fill our containing div
  76.     style.height = "100%";
  77.     style.width = "100%";
  78.     this.slider.appendChild(this._track);
  79.     
  80.     // Slider Track Left
  81.     element = document.createElement("div");
  82.     element.style.position = "absolute";
  83.     this._setObjectStart(element, 0);
  84.     this._track.appendChild(element);
  85.     
  86.     // Slider Track Middle
  87.     element = document.createElement("div");
  88.     element.style.position = "absolute";
  89.     this._track.appendChild(element);
  90.     
  91.     // Slider Track Right
  92.     element = document.createElement("div");
  93.     element.style.position = "absolute";
  94.     this._setObjectEnd(element, 0);
  95.     this._track.appendChild(element);
  96.     
  97.     // Slider Thumb
  98.     this._thumb = document.createElement("div");
  99.     style = this._thumb.style;
  100.     style.position = "absolute";
  101.     this._track.appendChild(this._thumb);
  102.     
  103.     this.setSize(this.size);
  104.     this.setTrackStart(this.trackStartPath, this.trackStartLength);
  105.     this.setTrackMiddle(this.trackMiddlePath);
  106.     this.setTrackEnd(this.trackEndPath, this.trackEndLength);
  107.     this.setThumb(this.thumbPath, this.thumbLength);
  108.     
  109.     this.slider.style.appleDashboardRegion = "dashboard-region(control rectangle)";
  110.     
  111.     // Add event listeners
  112.     this._track.addEventListener("mousedown", this._mousedownTrackHandler, true);
  113.     this._thumb.addEventListener("mousedown", this._mousedownThumbHandler, true);
  114.     
  115.     this.refresh();
  116. }
  117.  
  118. AppleSlider.prototype.remove = function()
  119. {
  120.     var parent = this._track.parentNode;
  121.     parent.removeChild(this._track);
  122. }
  123.  
  124. /*
  125.  * refresh() member function
  126.  * Refresh the current slider position and size.
  127.  * Call this to make the slider appear after the widget has loaded and 
  128.  * the AppleSlider object has been instantiated.
  129.  */
  130. AppleSlider.prototype.refresh = function()
  131. {
  132.     // get the scrollbar offset
  133.     this._trackOffset = this._computeTrackOffset();
  134.     this._sliderLength = this._computeSliderLength();
  135.     
  136.     this._numScrollablePixels = this._sliderLength - this.thumbLength - (2 * this.padding);
  137.     
  138.     this.slideTo(this._numScrollablePixels * this.value + this.padding);
  139. }
  140.  
  141. AppleSlider.prototype.slideTo = function(newThumbPos)
  142. {    
  143.     if (newThumbPos < this.padding)
  144.     {
  145.         newThumbPos = this.padding;
  146.     }
  147.     else if (newThumbPos > this._numScrollablePixels)
  148.     {
  149.         newThumbPos = this._numScrollablePixels;
  150.     }
  151.     
  152.     this.value = (newThumbPos - this.padding) / (this._numScrollablePixels - this.padding);
  153.     this._setObjectStart(this._thumb, newThumbPos);
  154.     
  155.     if (this.continuous && this.onchanged != null)
  156.         this.onchanged(this.value);
  157. }
  158.  
  159. // Capture events that we don't handle but also don't want getting through
  160. AppleSlider.prototype._captureEvent = function(event)
  161. {
  162.     event.stopPropagation();
  163.     event.preventDefault();
  164. }
  165.  
  166. /*********************
  167.  * Thumb scroll events
  168.  */
  169. AppleSlider.prototype._mousedownThumb = function(event)
  170. {
  171.     // temporary event listeners
  172.     document.addEventListener("mousemove", this._mousemoveThumbHandler, true);
  173.     document.addEventListener("mouseup", this._mouseupThumbHandler, true);
  174.     document.addEventListener("mouseover", this._captureEventHandler, true);
  175.     document.addEventListener("mouseout", this._captureEventHandler, true);
  176.     
  177.     this._thumbStartTemp = this._getMousePosition(event);
  178.     
  179.     this._sliderThumbStartPos = this._getThumbStartPos();
  180.  
  181.     event.stopPropagation();
  182.     event.preventDefault();
  183. }
  184.  
  185. AppleSlider.prototype._mousemoveThumb = function(event)
  186. {
  187.     var delta = this._getMousePosition(event) - this._thumbStartTemp
  188.     
  189.     var new_pos = this._sliderThumbStartPos + delta;
  190.     this.slideTo(new_pos);
  191.     
  192.     event.stopPropagation();
  193.     event.preventDefault();
  194. }
  195.  
  196. AppleSlider.prototype._mouseupThumb = function(event)
  197. {
  198.     document.removeEventListener("mousemove", this._mousemoveThumbHandler, true);
  199.     document.removeEventListener("mouseup", this._mouseupThumbHandler, true);
  200.     document.removeEventListener("mouseover", this._captureEventHandler, true);
  201.     document.removeEventListener("mouseout", this._captureEventHandler, true);
  202.     
  203.     // reset the starting position
  204.     delete this._thumbStartTemp;
  205.     delete this._sliderThumbStartPos;
  206.     
  207.     event.stopPropagation();
  208.     event.preventDefault();
  209.     
  210.     // Fire our onchanged event now if they have discontinuous event firing
  211.     if (!this.continuous && this.onchanged != null)
  212.         this.onchanged(this.value);
  213. }
  214.  
  215. /*********************
  216.  * Track scroll events
  217.  */
  218. AppleSlider.prototype._mousedownTrack = function(event)
  219. {
  220.     this._thumbStartTemp = this._getMousePosition(event);
  221.     this._sliderThumbStartPos = this._getMousePosition(event) - this._trackOffset - (this.thumbLength / 2);
  222.     
  223.     // temporary event listeners
  224.     document.addEventListener("mousemove", this._mousemoveThumbHandler, true);
  225.     document.addEventListener("mouseup", this._mouseupThumbHandler, true);
  226.     document.addEventListener("mouseover", this._captureEventHandler, true);
  227.     document.addEventListener("mouseout", this._captureEventHandler, true);
  228.     
  229.     this.slideTo(this._sliderThumbStartPos);
  230.  
  231. AppleSlider.prototype.setSize = function(size)
  232. {
  233.     this.size = size;
  234.     
  235.     this._setObjectSize(this.slider, size);
  236.     this._setObjectSize(this._track.children[1], size);
  237.     this._setObjectSize(this._thumb, size);
  238. }
  239.  
  240. AppleSlider.prototype.setTrackStart = function(imgpath, length)
  241. {
  242.     this.trackStartPath = imgpath;
  243.     this.trackStartLength = length;
  244.     
  245.     var element = this._track.children[0];
  246.     element.style.background = "url(" + imgpath + ") no-repeat top left";
  247.     this._setObjectLength(element, length);
  248.     this._setObjectSize(element, this.size);
  249.     this._setObjectStart(this._track.children[1], length);
  250. }
  251.  
  252. AppleSlider.prototype.setTrackMiddle = function(imgpath)
  253. {
  254.     this.trackMiddlePath = imgpath;
  255.     
  256.     this._track.children[1].style.background = "url(" + imgpath + ") " + this._repeatType + " top left";
  257. }
  258.  
  259. AppleSlider.prototype.setTrackEnd = function(imgpath, length)
  260. {
  261.     this.trackEndPath = imgpath;
  262.     this.trackEndLength = length;
  263.     
  264.     var element = this._track.children[2];
  265.     element.style.background = "url(" + imgpath + ") no-repeat top left";
  266.     this._setObjectLength(element, length);
  267.     this._setObjectSize(element, this.size);
  268.     this._setObjectEnd(this._track.children[1], length);
  269. }
  270.  
  271. AppleSlider.prototype.setThumb = function(imgpath, length)
  272. {
  273.     this.thumbPath = imgpath;
  274.     this.thumbLength = length;
  275.  
  276.     this._thumb.style.background = "url(" + imgpath + ") no-repeat top left";
  277.     this._setObjectLength(this._thumb, length);
  278. }
  279.  
  280. AppleSlider.prototype.setValue = function(newvalue)
  281. {
  282.     this.slideTo(this._numScrollablePixels * newvalue + this.padding);
  283. }
  284.  
  285. /*******************************************************************************
  286. * AppleHorizontalSlider
  287. * Implementation of AppleSlider
  288. *
  289. *
  290. */
  291.  
  292. function AppleHorizontalSlider(slider, onchanged)
  293. {
  294.     /* Objects */
  295.     this.slider = slider;
  296.     
  297.     /* public properties */
  298.     // These are read-write. Set them as needed.
  299.     this.onchanged = onchanged;
  300.     this.continuous = true; // Fire onchanged live, as opposed to onmouseup
  301.     this.padding = 0;
  302.     
  303.     // These are read-only. Use the setter functions to set them.
  304.     this.value = 0.0;
  305.     this.size = 22;
  306.     this.trackStartPath = "file:///System/Library/WidgetResources/AppleClasses/Images/slide_track_hleft.png";
  307.     this.trackStartLength = 8;
  308.     this.trackMiddlePath = "file:///System/Library/WidgetResources/AppleClasses/Images/slide_track_hmid.png";
  309.     this.trackEndPath = "file:///System/Library/WidgetResources/AppleClasses/Images/slide_track_hright.png";
  310.     this.trackEndLength = 8;
  311.     this.thumbPath = "file:///System/Library/WidgetResources/AppleClasses/Images/slide_thumb.png";
  312.     this.thumbLength = 23;
  313.     
  314.     /* Internal objects */
  315.     this._track = null;
  316.     this._thumb = null;
  317.     
  318.     /* Dimensions */
  319.     // these only need to be set during refresh()
  320.     this._trackOffset = 0;
  321.     this._sliderLength = 0;
  322.     this._numScrollablePixels = 0;
  323.     this._repeatType = "repeat-x";
  324.     
  325.     this._init();
  326. }
  327.  
  328. // Inherit from AppleSlider
  329. AppleHorizontalSlider.prototype = new AppleSlider(null);
  330.  
  331. /*********************
  332.  * Orientation-specific functions.
  333.  * These helper functions return vertical values.
  334.  */
  335. AppleHorizontalSlider.prototype._computeTrackOffset = function()
  336. {
  337.     // get the absolute left of the track
  338.     var obj = this.slider;
  339.     var curtop = 0;
  340.     while (obj.offsetParent)
  341.     {
  342.         curtop += obj.offsetLeft;
  343.         obj = obj.offsetParent;
  344.     }
  345.     
  346.     return curtop;
  347. }
  348.  
  349. AppleHorizontalSlider.prototype._computeSliderLength = function()
  350. {
  351.     // get the current actual slider length
  352.     var style = document.defaultView.getComputedStyle(this.slider, '');
  353.     return style ? parseInt(style.getPropertyValue("width"), 10) : 0;
  354. }
  355.  
  356. AppleHorizontalSlider.prototype._setObjectSize = function(object, size)
  357. { object.style.height = size + "px"; }
  358.  
  359. AppleHorizontalSlider.prototype._setObjectLength = function(object, length)
  360. { object.style.width = length + "px"; }
  361.  
  362. AppleHorizontalSlider.prototype._setObjectStart = function(object, start)
  363. { object.style.left = start + "px"; }
  364.  
  365. AppleHorizontalSlider.prototype._setObjectEnd = function(object, end)
  366. { object.style.right = end + "px"; }
  367.  
  368. AppleHorizontalSlider.prototype._getMousePosition = function(event)
  369. {
  370.     if (event != undefined)
  371.         return event.x;
  372.     else
  373.         return 0;
  374. }
  375.  
  376. AppleHorizontalSlider.prototype._getThumbStartPos = function()
  377. {
  378.     return parseInt(document.defaultView.getComputedStyle(this._thumb, '').getPropertyValue("left"), 10);
  379. }
  380.  
  381.  
  382. /*******************************************************************************
  383. * AppleVerticalSlider
  384. * Implementation of AppleSlider
  385. *
  386. *
  387. */
  388.  
  389. function AppleVerticalSlider(slider, onchanged)
  390. {
  391.     /* Objects */
  392.     this.slider = slider;
  393.     
  394.     /* public properties */
  395.     // These are read-write. Set them as needed.
  396.     this.onchanged = onchanged;
  397.     this.continuous = true; // Fire onchanged live, as opposed to onmouseup
  398.     this.padding = 0;
  399.     
  400.     // These are read-only. Use the setter functions to set them.
  401.     this.value = 0.0;
  402.     this.size = 22;
  403.     this.trackStartPath = "file:///System/Library/WidgetResources/AppleClasses/Images/slide_track_vtop.png";
  404.     this.trackStartLength = 8;
  405.     this.trackMiddlePath = "file:///System/Library/WidgetResources/AppleClasses/Images/slide_track_vmid.png";
  406.     this.trackEndPath = "file:///System/Library/WidgetResources/AppleClasses/Images/slide_track_vbottom.png";
  407.     this.trackEndLength = 8;
  408.     this.thumbPath = "file:///System/Library/WidgetResources/AppleClasses/Images/slide_thumb.png";
  409.     this.thumbLength = 23;
  410.     
  411.     /* Internal objects */
  412.     this._track = null;
  413.     this._thumb = null;
  414.     
  415.     /* Dimensions */
  416.     // these only need to be set during refresh()
  417.     this._trackOffset = 0;
  418.     this._sliderLength = 0;
  419.     this._numScrollablePixels = 0;
  420.     this._repeatType = "repeat-y";
  421.     
  422.     this._init();
  423. }
  424.  
  425. // Inherit from AppleSlider
  426. AppleVerticalSlider.prototype = new AppleSlider(null);
  427.  
  428. /*********************
  429. * Orientation-specific functions.
  430. * These helper functions return vertical values.
  431. */
  432. AppleVerticalSlider.prototype._computeTrackOffset = function()
  433. {
  434.     // get the absolute top of the track
  435.     var obj = this.slider;
  436.     var curtop = 0;
  437.     while (obj.offsetParent)
  438.     {
  439.         curtop += obj.offsetTop;
  440.         obj = obj.offsetParent;
  441.     }
  442.     
  443.     return curtop;
  444. }
  445.  
  446. AppleVerticalSlider.prototype._computeSliderLength = function()
  447. {
  448.     // get the current actual slider length
  449.     var style = document.defaultView.getComputedStyle(this.slider, '');
  450.     return style ? parseInt(style.getPropertyValue("height"), 10) : 0;
  451. }
  452.  
  453. AppleVerticalSlider.prototype._setObjectSize = function(object, size)
  454. { object.style.width = size + "px"; }
  455.  
  456. AppleVerticalSlider.prototype._setObjectLength = function(object, length)
  457. { object.style.height = length + "px"; }
  458.  
  459. AppleVerticalSlider.prototype._setObjectStart = function(object, start)
  460. { object.style.top = start + "px"; }
  461.  
  462. AppleVerticalSlider.prototype._setObjectEnd = function(object, end)
  463. { object.style.bottom = end + "px"; }
  464.  
  465. AppleVerticalSlider.prototype._getMousePosition = function(event)
  466. {
  467.     if (event != undefined)
  468.         return event.y;
  469.     else
  470.         return 0;
  471. }
  472.  
  473. AppleVerticalSlider.prototype._getThumbStartPos = function()
  474. {
  475.     return parseInt(document.defaultView.getComputedStyle(this._thumb, '').getPropertyValue("top"), 10);
  476. }
  477.